package org.ume.school.crawl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.*;

/**
 * Created by Django on 2017/9/2.
 */
public class BookCrawlRobot {

    public static String prefix = "http://www.xiexingcun.com/wenxueyuedu";

    public static void main(String[] args) throws IOException {
        String url = prefix + "/index.html";
        List<Book> bookList = loadBooks(url);
        System.out.println(bookList.size());
        for (Book book : bookList) {
            loadBookDetail(book);
        }
    }

    private static void loadBookDetail(Book book) {
        String detailUrl = prefix + "/" + book.getDetailUrl();
        String coverUrl = prefix + "/" + book.getCoverUrl();
        //加载封面
        String ncover = downloadCover(coverUrl);
        book.setCoverUrl(ncover);
        //解析

    }

    private static String downloadCover(String coverUrl) {

        return null;
    }


    public static List<Book> loadBooks(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        Elements els = doc.select("table[width=450] tr[valign=middle] td div[align=center]");
        HashMap<String, Book> bookMap = new HashMap<>();
        for (Element el : els) {
            List<Node> nodes = el.childNodes();
            if (nodes != null && nodes.size() > 0) {
                Node node = nodes.get(0);
                if (node instanceof TextNode) {
                } else {
                    Element a = (Element) node;
                    if (a != null) {
                        if ("a".equals(a.tagName().toLowerCase())) {
                            String href = a.attr("href");
                            boolean hasKey = bookMap.containsKey(href);
                            Book b = null;
                            if (hasKey) {
                                b = bookMap.get(href);
                            } else {
                                b = new Book();
                                b.setDetailUrl(href);
                                bookMap.put(href, b);
                            }
                            Elements c = a.children();
                            Element cx = c.first();
                            if (cx != null) {
                                if ("img".equals(cx.tagName())) {
                                    String coverUrl = cx.attr("src");
                                    b.setCoverUrl(coverUrl);
                                } else {
                                    String title = cx.text();
                                    b.setTitle(title);
                                }
                            } else {
                                String title = a.text();
                                b.setTitle(title);
                            }
                        }
                    }
                }
            }
        }
        return new ArrayList<Book>(bookMap.values());
    }

    public static class Book {

        private String title;

        private String coverUrl;

        private String detailUrl;

        private String author;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getCoverUrl() {
            return coverUrl;
        }

        public void setCoverUrl(String coverUrl) {
            this.coverUrl = coverUrl;
        }

        public String getDetailUrl() {
            return detailUrl;
        }

        public void setDetailUrl(String detailUrl) {
            this.detailUrl = detailUrl;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }
    }

}
